﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Sys.WebRes.Data;

namespace Sys.WebRes.Publish.Controllers
{
    public class AttachmentController : Controller
    {
        /// <summary>
        /// 获取附件数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public ActionResult Pic(int? id, string path, bool t = false)
        {
            if (t == true)
            {
                return Thumb(id, path);
            }

            var fullPath = GetFileFullPath(id, path);
            if (string.IsNullOrEmpty(fullPath))
            {
                var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SysConfig.UploadFolder);
                fullPath = Path.Combine(uploadFolder, SysConfig.DefaultPicture);
            }

            return File(fullPath, "image/*");
        }

        /// <summary>
        /// 获取资源缩略图
        /// 图片为图片缩略图
        /// 媒体为第一帧缩略图
        /// 文件为Icon图片
        /// </summary>
        /// <param name="id"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public ActionResult Thumb(int? id, string path)
        {
            var fullPath = GetFileFullPath(id, path);
            if (string.IsNullOrEmpty(fullPath))
            {
                var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SysConfig.UploadFolder);
                fullPath = Path.Combine(uploadFolder, SysConfig.DefaultPicture);
                return File(fullPath, "image/*");
            }

            var fileName = Path.GetFileName(fullPath);
            var dir = Path.GetDirectoryName(fullPath);
            var fileExt = Path.GetExtension(fileName);
            fileExt = string.IsNullOrEmpty(fileExt) ? "" : fileExt.Substring(1);

            if (ResType.Picture.Ext.Contains(fileExt))
            {
                fullPath = Path.Combine(dir, SysConfig.ThumbPrefix + fileName);
            }
            else
            {
                fullPath = Path.Combine(dir, SysConfig.ThumbPrefix + fileName + ".jpg");
            }

            return File(fullPath, "image/*");
        }

        /// <summary>
        /// 获取文件路径
        /// </summary>
        /// <param name="id"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        private string GetFileFullPath(int? id, string path)
        {
            if (id > 0)
            {
                using (var db = DB.New)
                {
                    var entity = db.AttachmentTBL.FirstOrDefault(p => p.ID == id);
                    if (entity != null)
                    {
                        path = Path.Combine(entity.Folder, entity.FileName);
                    }
                }
            }

            if (string.IsNullOrEmpty(path))
            {
                return null;
            }

            var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SysConfig.UploadFolder);
            var sFullPath = Path.Combine(uploadFolder, path);
            return sFullPath;
        }
    }
}
